package com.java.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyTools {

	public static final String Date = "yyyy-MM-dd";
	public static final String DateTime = "yyyy-MM-dd HH:mm:ss";
	public static final String DateTimeSub = "yyyy-MM-dd HH:mm";

	public static String getNowDate() {
		return new SimpleDateFormat(Date).format(Calendar.getInstance().getTime());
	}

	public static String getNowDateTime() {
		return new SimpleDateFormat(DateTime).format(Calendar.getInstance().getTime());
	}

	public static String getNowDateTimeSub() {
		return new SimpleDateFormat(DateTimeSub).format(Calendar.getInstance().getTime());
	}
	
	public static Date formatDate (String dateStr) {
		SimpleDateFormat formatter = new SimpleDateFormat(Date);
		try {
			return formatter.parse(dateStr);
			//return new SimpleDateFormat(Date).format(date);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	public static Date formatDateTime (String dateTimeStr) {
		SimpleDateFormat formatter = new SimpleDateFormat(DateTime);
		try {
			return formatter.parse(dateTimeStr);
			//return new SimpleDateFormat(DateTime).format(date);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	public static String date2Str(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public static boolean isLegalDate(int length, String sDate, String format) {
		int legalLen = length;
		if ((sDate == null) || (sDate.length() != legalLen)) {
			return false;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			Date date = formatter.parse(sDate);
			return sDate.equals(formatter.format(date));
		} catch (Exception e) {
			return false;
		}
	}
}
